package com.yyp.car.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yyp.car.Iservice.IGroundService;
import com.yyp.car.Iservice.ISchoolService;
import com.yyp.car.Iservice.IStudentService;
import com.yyp.car.Iservice.ITestTypeService;
import com.yyp.car.Iservice.IUserService;
import com.yyp.car.model.Ground;
import com.yyp.car.model.School;
import com.yyp.car.model.Student;
import com.yyp.car.model.User;

@Controller("studentController")
@RequestMapping("/student")
public class StudentController {
	@Resource(name = "studentService")
	private IStudentService studentService;
	@Resource(name = "groundService")
	private IGroundService groundService;

	@Resource(name = "testTypeService")
	private ITestTypeService testTypeService;

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "schoolService")
	private ISchoolService schoolService;

	@RequestMapping(value = "/studentRegister", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	public @ResponseBody String studentRegister(Student student, User user, int sid, String conPassword) {
		if (!conPassword.equals(user.getPassword())) {
			return "两次密码不一致";
		}
		School school = schoolService.load(sid);
		student.setSchool(school);
		user.setStatus(0);
		user.setStudentMes(student);
		studentService.registerStudent(student);
		userService.register(user);
		return "ok";
	}

	@RequestMapping(value = "/delTestType", method = RequestMethod.POST)
	public String delTestType(HttpSession session) {
		User user = (User) session.getAttribute("user");
		user.setTtid(null);
		user.setTestType(null);
		studentService.cancleTestMes(user.getStudentMes().getId());
		Student student = studentService.findStudet(user.getStudentMes().getId());
		user.setStudentMes(student);
		user.setTeacher(null);
		userService.update(user);
		session.setAttribute("user", user);
		return "redirect:/index/jszksyy";
	}

	@RequestMapping(value = "/addTestType", method = RequestMethod.POST)
	public String addTestType(int tid, String testType, HttpSession session) {
		User user = (User) session.getAttribute("user");
		user.setTtid(tid);
		user.setTestType(testType);
		studentService.cancleTestMes(user.getStudentMes().getId());
		Student student = studentService.findStudet(user.getStudentMes().getId());
		user.setStudentMes(student);
		user.setTeacher(null);
		userService.update(user);
		session.setAttribute("user", user);
		return "redirect:/index/jszksyy";
	}

	@RequestMapping(value = "/addTestPlace", method = RequestMethod.GET)
	public ModelAndView addTestPlace() {
		ModelAndView ma = new ModelAndView("/ajaxViews/addPlace");
		List<Ground> grounds = groundService.listGround();
		List<User> teachers = userService.listTeacher();
		ma.addObject("teachers", teachers);
		ma.addObject("grounds", grounds);
		return ma;
	}

	@RequestMapping(value = "/addTestPlace", method = RequestMethod.POST)
	public String addTestPlacep(int smid, int sid, int tid, String testPlace, String beginTime, String endTime) {
		studentService.updateTestMes(smid, testPlace, beginTime, endTime);
		User student = userService.findById(sid);
		User teacher = userService.findById(tid);
		student.setTeacher(teacher);
		userService.update(student);
		return "redirect:/back/studentManager";
	}

	@RequestMapping(value = "/editTestPlace", method = RequestMethod.GET)
	public ModelAndView editTestPlace(int smid, int sid) {
		ModelAndView ma = new ModelAndView("/ajaxViews/editPlace");
		List<Ground> grounds = groundService.listGround();
		Student studentMes = studentService.findTestMes(smid);
		List<User> teachers = userService.listTeacher();
		User student = userService.findById(sid);
		ma.addObject("teacherId", student.getTeacher().getId());
		ma.addObject("teachers", teachers);
		ma.addObject("studentMes", studentMes);
		ma.addObject("grounds", grounds);
		return ma;
	}

	@RequestMapping(value = "/canclePlan", method = RequestMethod.POST)
	public String cancleTestPlace(int smid, int sid) {
		studentService.updateTestMes(smid, "", "", "");
		User student = userService.findById(sid);
		student.setTeacher(null);
		userService.update(student);
		return "redirect:/back/studentManager";
	}
	@RequestMapping(value = "/blackStudent", method = RequestMethod.POST)
	public String blackStudent(int id) {
		userService.blackStudent(id);
		return "redirect:/back/blackManager";
	}
	@RequestMapping(value = "/whiteStudent", method = RequestMethod.POST)
	public String whiteStudent(int id) {
		userService.whiteStudent(id);
		return "redirect:/back/blackManager";
	}
	
	
}
